package de.ret_world.openrpgstudio.engine;

import buoy.event.CommandEvent;
import buoy.event.WindowClosingEvent;
import buoy.widget.*;
import de.ret_world.openrpgstudio.editor.Media;
import de.ret_world.openrpgstudio.editor.Project;
import de.ret_world.openrpgstudio.editor.helpers.Internationalization;
import de.ret_world.openrpgstudio.editor.plugininterfaces.ToolPlugin;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import org.jrabbit.base.managers.window.controllers.DesktopWindowController;

/**
 * Shows a GUI with a title image and options for resolution. You can use arguments to start the game directly.
 *
 * @author Emu
 */
public class GameLauncher extends ToolPlugin {

    /**
     * This plug-in ID.
     */
    public static final String PLUGIN_ID = "de.ret_world.openrpgstudio.engine";
    //Variables
    private BFrame parent;
    private BDialog dialog;
    private BComboBox cbResolution;
    private String[] resolutions = new String[]{"800x600", "1024x600", "1024x640", "1024x768", "1093x614", "1152x720", "1152x864", "1280x720", "1280x768", "1280x800", "1280x960", "1280x1024", "1360x768", "1366x768", "1440x900", "1600x900", "1680x1050", "1920x1080", "1920x1200", "2560x1440"};
    private BCheckBox cbxFullscreen;
    
    @Override
    public void init(BMenuItem menu, BButton button, BFrame parent) {
        this.parent = parent;
        
        menu.addEventLink(CommandEvent.class, this, "showGUI");
        button.addEventLink(CommandEvent.class, this, "showGUI");
    }
    
    private void showGUI() {
        Internationalization intn = Internationalization.getInstance();
        Project project = Project.getInstance();
        
        dialog = new BDialog(parent, intn.getString("GE.LAUNCH") + " " + project.getConfig().getProperty("game.name"), true);
        dialog.addEventLink(WindowClosingEvent.class, this, "close");
        
        BorderContainer bc = new BorderContainer();

        //Loading title image
        String img = project.getConfig().getProperty("game.title.img", "default");
        URL uicon = null;
        if (img.equals("default")) {
            uicon = manager.getPluginClassLoader(manager.getRegistry().getPluginDescriptor(PLUGIN_ID)).getResource("default_title.png");
        } else {
            Media m = project.getMediaManager().getMedia(img);
            try {
                uicon = m.getFile().toUri().toURL();
            } catch (MalformedURLException ex) {
                log.error(ex);
            }
        }
        
        BLabel image = new BLabel();
        if (uicon != null) {
            ImageIcon imgI = new ImageIcon(uicon);
            image.setIcon(imgI);
        }
        
        
        bc.add(BOutline.createEtchedBorder(image, false), BorderContainer.NORTH, new LayoutInfo(LayoutInfo.CENTER, LayoutInfo.BOTH, new Insets(5, 5, 5, 5), null));
        
        LayoutInfo li = new LayoutInfo(LayoutInfo.CENTER, LayoutInfo.HORIZONTAL, new Insets(5, 5, 5, 5), null);
        
        RowContainer rc = new RowContainer();
        rc.add(new BLabel(intn.getString("GE.RESOLUTION")), li);
        cbResolution = new BComboBox(resolutions);
        rc.add(cbResolution, li);
        cbxFullscreen = new BCheckBox(intn.getString("GE.FULLSCREEN"), false);
        rc.add(cbxFullscreen, li);
        BButton btn = new BButton(intn.getString("GE.LAUNCH"));
        btn.addEventLink(CommandEvent.class, this, "launch");
        rc.add(btn, li);
        bc.add(rc, BorderContainer.SOUTH, li);

        //Default values
        cbResolution.setSelectedValue(project.getConfig().getProperty("video.resolution", "1024x768"));
        cbxFullscreen.setState(Boolean.valueOf(project.getConfig().getProperty("video.fullscreen", "false")));
        
        dialog.setContent(bc);
        //dialog.setBounds(new Rectangle(150, 150, 660, 480));
        dialog.pack();
        dialog.setResizable(false);
        dialog.setVisible(true);
    }
    
    @Override
    public Shortcut getShortcut() {
        return new Shortcut(KeyEvent.VK_T, Shortcut.CTRL_MASK);
    }
    
    private void close() {
        dialog.dispose();
    }
    
    private void launch() {
        Project project = Project.getInstance();
        project.getConfig().setProperty("video.resolution", (String) cbResolution.getSelectedValue());
        project.getConfig().setProperty("video.fullscreen", Boolean.toString(cbxFullscreen.getState()));
        
        dialog.dispose();
        
        log.info("Initialize GameEngine...");
        
        String[] str = ((String) cbResolution.getSelectedValue()).split("x");
        
        int width = Integer.parseInt(str[0]);
        int height = Integer.parseInt(str[1]);
        boolean fullscreen = cbxFullscreen.getState();
        
        Game.init(new DesktopWindowController(width, height, fullscreen));
        
        log.info("Starting GameEngine...");
        new Game(project.getConfig().getProperty("game.name")).run();
    }
}
